package com.hsnn.medstgmini.base.std.controller;

import java.lang.reflect.Type;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.hsnn.medstgmini.Constants;
import com.hsnn.medstgmini.base.std.model.StdDrugInfo;
import com.hsnn.medstgmini.base.std.model.form.StdDrugInfoForm;
import com.hsnn.medstgmini.base.std.service.StdDrugInfoManager;
import com.hsnn.medstgmini.sys.model.SysUser;
import com.hsnn.medstgmini.util.Pagination;
import com.hsnn.medstgmini.util.PinYin;

@Controller
@RequestMapping("/stdDrug")
public class StdDrugController {
	@Autowired
	private StdDrugInfoManager stdDrugInfoManager;
	@Autowired
	private  HttpServletRequest request;
	@RequestMapping("/list")
	public String toDrugList(){
		return "/base/std/drugList";
	}
	/**
	 * 
	 *@category 查询所有药品列表（json格式）
	 *@author qiulei
	 *@date 2015年6月9日 下午5:42:51
	 *@return
	 *@return Pagination
	 */
	@RequestMapping("/getData" )
	@ResponseBody
	public Pagination getData(StdDrugInfoForm form){
		Pagination page = new Pagination(request);
		String prodPompId = request.getParameter("prodPompId");
		if(StringUtils.isNotBlank(form.getDrugId().trim())){
			page.getConditions().put("drugId", form.getDrugId().trim());
		}
		if (null != form.getDrugCode() && !"".equals(form.getDrugCode().trim())){
			page.getConditions().put("drugCode", form.getDrugCode().trim());
		}
		if (null != form.getDrugName() && !"".equals(form.getDrugName().trim())){
			if (PinYin.isPinYin(form.getDrugName().trim())){
				page.getConditions().put("drugNameSpel", form.getDrugName().trim());
			} else {
				page.getConditions().put("drugName", form.getDrugName().trim());
			}
		}
		if (null != form.getFormId() && !"".equals(form.getFormId().trim())){
			page.getConditions().put("formId", form.getFormId().trim());
		}
		if (null != form.getSpecId() && !"".equals(form.getSpecId().trim())){
			page.getConditions().put("specId", form.getSpecId().trim());
		}
		if (null != form.getProdPompId() && !"".equals(form.getProdPompId().trim())){
			page.getConditions().put("prodPompId", form.getProdPompId().trim());
		}
		return stdDrugInfoManager.getAllByPage(page);
	}
	/**
	 * 
	 *@category 启用停用
	 *@author qiulei
	 *@date 2015年6月9日 上午9:58:16
	 * @param drugId 药口ID
	 * @param status 状态：0停用，1启用
	 * @return
	 *@return String
	 */
	@RequestMapping("/drugStartOrStop" )
	@ResponseBody
	public String drugStartOrStop(String drugId,Short status){
		SysUser user=(SysUser) request.getSession().getAttribute(Constants.USERINFO);
		return stdDrugInfoManager.startOrStop(drugId, status,user.getId());
	}
	
	@RequestMapping("/batchStartOrStop" )
	@ResponseBody
	public String batchStartOrStop(String array){
		
		if (array != null && !"".equals(array)) {
			Gson gson = new Gson();
			Type type = new TypeToken<List<StdDrugInfo>>() {
			}.getType();
			List<StdDrugInfo> list = gson.fromJson(array, type);
			SysUser user=(SysUser) request.getSession().getAttribute(Constants.USERINFO);
			if (list != null && !list.isEmpty()) {
				for (StdDrugInfo dp : list) {
					dp.setUpdUser(user.getId());
				}
				return stdDrugInfoManager.batchStartOrStop(list);
			}
		}
		
		return "failed";
		
	}
	
	
	/**
	 *@category 跳向新增页面
	 *@param
	 *hsnn
	 *2015年6月9日 上午9:51:02
	 * @return
	 *@return String
	 */
	@RequestMapping(value ="/toAdd" )
	public String toAdd(){
		return "/base/std/drugAdd";
	}
	@RequestMapping(value ="/addInfo" )
	@ResponseBody
	public String addData(StdDrugInfoForm form){
		SysUser user=(SysUser) request.getSession().getAttribute(Constants.USERINFO);
		form.setUpdUser(user.getId());
		return stdDrugInfoManager.addData(form);
	}
	@RequestMapping(value ="/toEdit" )
	public String toEdit(){
		String drugid = request.getParameter("drugId");
		StdDrugInfoForm form = stdDrugInfoManager.getStdInfo(drugid);
		request.setAttribute("form", form);
		return "/base/std/drugEdit";
	}
	@RequestMapping(value ="/editInfo" )
	@ResponseBody
	public String editInfo(StdDrugInfoForm form){
		SysUser user=(SysUser) request.getSession().getAttribute(Constants.USERINFO);
		form.setUpdUser(user.getId());
		return stdDrugInfoManager.editInfo(form);
	}
	@RequestMapping(value ="/drugInfoDetail" )
	public String drugInfoDetail(Model model, String drugId){
		
		StdDrugInfoForm form = stdDrugInfoManager.getStdInfo(drugId);
		model.addAttribute("form", form);
		return "/base/std/drugInfoDetail";
	}
	
	@RequestMapping(value ="/drugInfoDetailPop" )
	public String drugInfoDetailPop(Model model, String drugId){
		StdDrugInfoForm form = stdDrugInfoManager.getStdInfo(drugId);
		model.addAttribute("form", form);
		return "/base/std/drugInfoDetailPop";
	}
	
	@RequestMapping(value ="/loglist" )
	public String toLogList(Model model, String drugId){
		model.addAttribute("drugId", drugId);
		return "/base/std/drugHisList";
	}
	@RequestMapping("/getDrugInfoLogList" )
	@ResponseBody
	public Pagination getDrugInfoLogList(String drugId ){
		Pagination page = new Pagination(request);
		if (null != drugId && !"".equals(drugId.trim())){
			page.getConditions().put("drugId", drugId);
		}
		if (null != request.getParameter("endTime") && !"".equals(request.getParameter("endTime"))){
			page.getConditions().put("endTime", request.getParameter("endTime"));
		}
		if (null != request.getParameter("startTime") && !"".equals(request.getParameter("startTime"))){
			page.getConditions().put("startTime", request.getParameter("startTime"));
		}
		return stdDrugInfoManager.getDrugInfoLogList(page);
	}
	@RequestMapping(value ="/drugInfoLogDetail" )
	public String drugInfoLogDetail(Model model, String Logid){
		StdDrugInfoForm form = stdDrugInfoManager.drugInfoLogDetail(Logid);
		StdDrugInfoForm oform = stdDrugInfoManager.getPreviousLog(form.getDrugId(), form.getUpdDatetime());
		model.addAttribute("form", form);
		model.addAttribute("oform", oform);
		return "/base/std/drugInfoLogDetail";
	}
	/**
	 * 
	 *@category 跳向合并页面
	 *@author qiulei
	 *@date 2015年6月9日 下午2:48:58
	 *@param drugId
	 *@return
	 *@return String
	 */
	@RequestMapping(value ="/toMergeDrugInfo" )
	public String toMergeDrugInfo(Model model,String drugId){
		StdDrugInfoForm form = stdDrugInfoManager.getMergeDrugInfo(drugId);
		model.addAttribute("form", form);
		return "/base/std/drugInfoMerge";
	}
	/**
	 * 
	 *@category 获取状态启用，并且排除被合并的列表
	 *@author qiulei
	 *@date 2015年6月9日 下午3:10:58
	 *@param drugId
	 *@return
	 *@return Pagination
	 */
	@RequestMapping("/getValidDrugInfoList" )
	@ResponseBody
	public Pagination getValidDrugInfoList(StdDrugInfoForm form){
		Pagination page = new Pagination(request);
		page.getConditions().put("drugId", form.getDrugId());
		if (null != form.getDrugCode() && !"".equals(form.getDrugCode().trim())){
			page.getConditions().put("drugCode", form.getDrugCode().trim());
		}
		if (null != form.getDrugName() && !"".equals(form.getDrugName().trim())){
			page.getConditions().put("drugName", form.getDrugName().trim());
		}
		if (null != form.getDrugform1() && !"".equals(form.getDrugform1().trim())){
			page.getConditions().put("drugform1", form.getDrugform1().trim());
		}
		if (null != form.getDrugform2() && !"".equals(form.getDrugform2().trim())){
			page.getConditions().put("drugform2", form.getDrugform2().trim());
		}
		if (null != form.getProdPompName() && !"".equals(form.getProdPompName().trim())){
			page.getConditions().put("prodPompName", form.getProdPompName().trim());
		}
		
		return stdDrugInfoManager.getValidDrugInfoList(page);
	}
	/**
	 * 
	 *@category 合并药品信息操作
	 *@author qiulei
	 *@date 2015年6月9日 下午4:41:45
	 *@param drugOId
	 *@param drugId
	 *@return
	 *@return String "success":成功。"failed":失败
	 */
	@RequestMapping("/drugInfoMerge" )
	@ResponseBody
	public String drugInfoMerge(String drugOId,String drugId){
		return stdDrugInfoManager.drugInfoMerge(drugOId, drugId);
	}
	@RequestMapping("/toSelectComp" )
	public String toSelectComp(){
		return "/base/selectPage/selectCompInfo";
	}

}
